﻿/*******************************************************/
//算法测试插件
//2024.1.31
//温
//仅包含默认返回初始值
/*******************************************************/
#pragma once

#include "ampackalgplugin_global.h"
#include <qvariant.h>
#include <qdatetime.h>
#include "algorithmplugin.h"
#include "files/algoxmloperate.h"
#include <qthreadpool.h>


class ClsCalculateAB;
class PolyCheck;
/// <summary>
/// 算法插件
/// </summary>
class AMPACKALGPLUGIN_EXPORT AmPackAlgPlugin : public AlgorithmPlugin
{
public:
	AmPackAlgPlugin();
	~AmPackAlgPlugin();
	/// <summary>
	/// 检测
	/// </summary>
	/// <param name="vecResult">输出结果结构体</param>
	/// <param name="images">输入图片结构体</param>
	/// <param name="params">暂时没用</param>
	void detect(QVector<DetectResult_t>& vecResult, const QVector<ImageData_t>& images, QList<algoItemStruct>& params) override;
	/// <summary>
	/// 算法操作
	/// </summary>
	/// <param name="operate">操作类型</param>
	/// <returns></returns>
	bool algoOperate(emAlgoOperate operate) override;
	/// <summary>
	/// 获取图片标记列表
	/// </summary>
	/// <returns>图片标记列表</returns>
	QStringList getAlgoMarkList() override;

	void tifImage16ToGrayImage8(const cv::Mat& ImageDepth, cv::Mat& ImgGray) override { return; }


	bool GetPolyWorkIsOpen();

	void SetParam();

private:
	/// <summary>
	/// 读取excel
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="sheet">表名</param>
	/// <returns>行数据variant列表</returns>
	QVariantList ReadXls(const QString& path, int sheetIndex = 1);
	QStringList ReadCsv(const QString& path);
	/// <summary>
	/// 读位姿表xlsx
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="value">返回值</param>
	/// <param name="sheet">表名</param>
	/// <returns></returns>
	bool ReadXlsPos(const QString& path, QMap<QString, QStringList>& value, int sheetIndex = 1);
	/// <summary>
	/// 重载读位姿表xlsx
	/// </summary>
	/// <param name="varRows">行数据variant列表</param>
	/// <param name="value">返回值</param>
	void ReadXlsPos(const QVariantList& varRows, QMap<QString, QStringList>& value);
	/// <summary>
	/// 读检测结果表
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="value">值</param>
	/// <param name="sheet">表名</param>
	/// <returns></returns>
	bool ReadCsvRes(const QString& path, QVector<DetectDefectInfo_t>& value);
	bool ReadXlsRes(const QString& path, QVector<DetectDefectInfo_t>& value, int sheetIndex = 1);
	/// <summary>
	/// 重载读检测结果表
	/// </summary>
	/// <param name="varRows">行数据variant列表</param>
	/// <param name="value">返回值</param>
	void ReadCsvRes(const QStringList& varRows, QVector<DetectDefectInfo_t>& value);
	void ReadXlsRes(const QVariantList& varRows, QVector<DetectDefectInfo_t>& value);
	/// <summary>
	/// 读相机标定矩阵
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="matrix">矩阵</param>
	/// <returns></returns>
	bool ReadCamMat(const QString& path, QMap<QString, cv::Mat>& map);
	/// <summary>
	/// 读机器人基坐标
	/// </summary>
	/// <param name="path"></param>
	/// <param name="posbase"></param>
	/// <returns></returns>
	bool ReadPosBase(const QString& path, QStringList& posbase);
	/// <summary>
	/// 读取变换矩阵c
	/// </summary>
	/// <param name="path">路径</param>
	/// <param name="matrix">矩阵</param>
	/// <returns></returns>
	bool ReadTransCMatirx(const QString& path, cv::Mat& matrix);
	/// <summary>
	/// 矩阵转字符串，日志用
	/// </summary>
	/// <param name="matrix">矩阵</param>
	/// <returns>日志字符串</returns>
	QString MatToString(const cv::Mat& matrix);
	/// <summary>
	/// 更新状态值
	/// </summary>
	/// <param name="matrix">矩阵</param>
	/// <returns>日志字符串</returns>
	void UpdateFovVal(QString strAlgoMark);
	bool DeleteFileOrFolder(const QString& strPath);
	//算法模块
	void JingDingWei(const ImageData_t& image, DetectResult_t& detectRes, int index, bool isHigh);
	void YiBanDianWei(const ImageData_t& image, DetectResult_t& detectRes, bool isHigh);
	void Polyworks(const ImageData_t& image, DetectResult_t& detectRes, bool isHigh);
	void FirstCheck(const ImageData_t& image, DetectResult_t& detectRes);


	QStringList mAlgoMarkList;//图片标记列表
	QMap<QString, QStringList> mPosMap;//所有拍照位姿
	QMap<QString, cv::Mat> mPosMatrixLow;//所有拍照位姿
	QMap<QString, cv::Mat> mPosMatrixHigh;//所有拍照位姿

	QMap<QString, QString> mAlgParam;//检测参数

	QStringList mEmptyPose{
		"","","","0","0",
		"0","0","0","0","0","0","0",
		"a","a","0","0","0","","",""
	};

	cv::Mat mTransB;
	cv::Mat mTransC;
	cv::Mat mTransC2;
	cv::Mat mIdentityMatrix;//单位矩阵
	

	QMap<QString, cv::Mat> mCamMatrixMap;//type，相机标定mat
	QStringList mPosBase;//机器人基准位姿

	ClsCalculateAB* mpCalAB;
	PolyCheck* mpPolyCheck;

	QString mCurrentFormula;//当前配方路径
	std::atomic_bool mInitFlag{ false };//是否初始化

	QThreadPool mPool;

	//v20241209
	QVector<unsigned int> mvnRandSeed{ 2036570917,1668589894,251121870,4038525836,1279133660,2651262881,28038134,\
		31402454,656459663,1135628964,3836885,1081311228,204993870,3549425559,1668817723,364286067,3234199954,\
		3875224759,2294253151,1044953255,1340434912,3308515843,1940516132,79983554,590809645,196737798,1806111571,\
		3793142933,2783691062,350895699,905840796,3158262302,1532514593,389964983,670399986,374825521,951905546,\
		471546464,934103115,2135282097,243737960,1175695710,1069300867,138555538,2264564893,1387646910,2523056786,\
		7116689,4172591912,3312784903,1777640702,1424268844,1672939224,109482214,2663005605,2236537227,3415180121,\
		3869326875,1091345788,858488635,3986649884,1413803523,3649566136,1061963454,457404180,1184270590,2956893589,\
		2447410854,126611872,1812174355,24039424,3044232809,1322752427,547470675,909114370,2623326629,3517304195,\
		3094223548,872599629,1207834998,1292384999,769886434,1662564081,490773144,2865837772,462229133,4080361320,\
		729201224,3380675579,179900277,32157040,1250221165,1332890750,1332862782,3818837138,1179254836,896962051,\
		1324738531,711608478,123923047,3539690085,2748942467,3383135503,432558879,1020400565,

	};
	unsigned int mnRandSeedIndex;
	float mfOffsetX, mfOffsetY, mfOffsetZ;
	bool mbUseLocalData;
	float mfRandVal0 = 0, mfRandVal1 = 0, mfRandVal2 = 0;
	QString mqstrMarkLast = "";
};

extern "C"
{
	AMPACKALGPLUGIN_EXPORT Plugin* createPlugin(QObject* parent);


	AMPACKALGPLUGIN_EXPORT void releasePlugin(Plugin** pPlugin);
}
